<template>
  <div class="app-container">
    <div class="block">
      <el-table
        ref="table"
        :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
        row-key="id"
        border
        lazy
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          prop="name"
          label="权限名称"
          width="200"
        />
        <el-table-column
          prop="auth"
          label="权限值"
          width="300"
        />
        <el-table-column
          prop="url"
          label="预览URL参数"
          width="300"
        />
        <el-table-column
          align="right"
          width="300"
        >
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              style="width: 160px"
              placeholder="关键字搜索一级菜单"
            />
            <el-button
              size="mini"
              @click="handleCreate(-1, {id:0})"
            >添加一级菜单</el-button>
          </template>
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleCreate(scope.$index, scope.row)"
            >添加</el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button>

            <el-popconfirm
              title="确认删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                slot="reference"
                size="mini"
                type="danger"
              >删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="权限名称" prop="title">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="权限值" prop="title">
          <el-input v-model="temp.auth" />
        </el-form-item>
        <el-form-item label="预览URL参数" prop="title">
          <el-input v-model="temp.url" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          保存
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { getAuthsTree, addeditAuthNode, delAuthNode } from '@/api/auth'

export default {
  name: 'AuthList',
  data() {
    return {
      tableData: [],
      search: '',
      dialogFormVisible: false,
      index: -1,
      temp: {
        id: undefined,
        name: '',
        auth: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑框',
        create: '新增框'
      },
      pid: undefined
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getAuthsTree().then(result => {
        this.tableData = result.map.list
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: '',
        auth: ''
      }
    },
    handleCreate(index, row) {
      this.resetTemp()
      this.pid = row.id
      this.index = index
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      const tempData = Object.assign({}, this.temp)
      tempData.optType = 'add'
      tempData.pid = this.pid
      tempData.text = tempData.name
      console.log(tempData)
      addeditAuthNode(tempData).then((result) => {
        if (result.code === 1) {
          this.dialogFormVisible = false
          this.refreshRow(tempData.pid)
          this.$notify({
            title: '成功',
            message: '保存成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },

    handleEdit(index, row) {
      this.temp = Object.assign({}, row) // copy obj
      this.index = index
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      console.log(index, row)
    },

    updateData() {
      const tempData = Object.assign({}, this.temp)
      tempData.children = undefined
      tempData.optType = 'edit'
      tempData.text = tempData.name
      console.log(tempData)
      addeditAuthNode(tempData).then((result) => {
        if (result.code === 1) {
          this.dialogFormVisible = false
          this.refreshRow(this.temp.menuParentId)

          this.$notify({
            title: 'Success',
            message: '保存成功',
            type: 'success',
            duration: 2000
          })
        }
      })
    },
    refreshRow(id) {
      getAuthsTree({ id: id }).then(result => {
        if (result.success) {
          this.$set(this.$refs.table.store.states.lazyTreeNodeMap, id, result.map.list)
        }
      })
    },

    handleDelete(index, row) {
      delAuthNode({ id: row.id }).then(result => {
        this.refreshRow(row.menuParentId)
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    }

  }
}
</script>
<style scoped>

</style>
